package com.smallA.service.Impl;

import com.smallA.constant.AccessConstant;
import com.smallA.dao.UserMapper;
import com.smallA.po.Access;
import com.smallA.po.Classroom;
import com.smallA.po.Client;
import com.smallA.po.Test;
import com.smallA.service.UserService;
import com.smallA.utils.DataUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户登录等操作的实现类
 *
 * @author 君未洋
 */
public class UserServiceImpl implements UserService {

    private UserMapper userMapper;
    private BCryptPasswordEncoder passwordEncoder;
    private DataUtils dataUtils;

    public void setDataUtils(DataUtils dataUtils) {
        this.dataUtils = dataUtils;
    }

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public void setPasswordEncoder(BCryptPasswordEncoder passwordEncoder) {
        //为加密类动态赋值
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //注意这里是需要先使用自己的用户数据库查询出对应的用户账号,转化为SpringSecurity对应的用户对象
        try {
            Client user = userMapper.selectClientByName(username);
            if (user == null) {
                return null;
            }
            List<SimpleGrantedAuthority> authorities = new ArrayList<>();
            //这里就是添加一个对应的角色到这里的list集合中了
            List<Access> accesses = user.getRole().getAccesses();
            for (Access access : accesses) {
                authorities.add(new SimpleGrantedAuthority(access.getName()));
            }
            //这里就是动态将对应的角色导入到这里面
            UserDetails userDetails = new User(user.getUsername(), user.getPassword(), authorities);
            return userDetails;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
            //在SpringSecurity中如果返回的是null就代表认证失败
        }
    }

    @Override
    public List<Client> selectAllUser() {
        return userMapper.selectAllUser();
    }

    @Override
    public Client selectClientById(Integer id) {
        if (dataUtils.examineNumber(id)) {
            return null;
        }
        return userMapper.selectClientById(id);
    }

    @Override
    public Client selectClientLogin(String username, String password) {
        if (username == null || "".equals(username) || "".equals(password) || password == null) {
            return null;
        }
        return userMapper.selectClientLogin(username, password);
    }

    @Override
    public int deleteClient(Integer id) {
        if (dataUtils.examineNumber(id)) {
            return 0;
        }
        return userMapper.deleteClient(id);
    }

    @Override
    public int insertClient(Client client) {
        if (client == null) {
            return 0;
        }
        client.setPassword(passwordEncoder.encode(client.getPassword()));
        //对用户密码加密后再存入数据库
        return userMapper.insertClient(client);
    }

    @Override
    public Client selectUserByName(String username) {
        if (username == null || "".equals(username)) {
            return null;
        }
        return userMapper.selectClientByName(username);
    }

    @Override
    public List<Classroom> selectClassById(Integer id) {
        if (dataUtils.examineNumber(id)) {
            return null;
        }
        return userMapper.selectClassById(id);
    }

    @Override
    public List<Client> selectStuByClassId(Integer id) {
        if (dataUtils.examineNumber(id)) {
            return null;
        }
        List<Integer> list = userMapper.selectStuByClassId(id);
        List<Client> clientList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            clientList.add(userMapper.selectClientById(list.get(i)));
        }
        return clientList;
    }

    @Override
    public List<Test> selectTestByClassId(Integer id) {
        if (dataUtils.examineNumber(id)) {
            return null;
        }
        List<Test> testList = userMapper.selectTestByClassId(id);
        return testList;
    }

    @Override
    public Classroom selectClassByClassId(Integer id) {
        if (dataUtils.examineNumber(id)) {
            return null;
        }
        return userMapper.selectClassByClassId(id);
    }

    @Override
    public String judge(Client client) {
        if (client == null){
            return "login";
        }
        if(client.getRole().getRoleName().equals(AccessConstant.STUDENT_ROLE)){
            return "student/main";
        }else{
            return "teacher/main";
        }
    }
}
